package com.cap.bts.framework.auth.interceptor;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

public class AccessLog implements Serializable {

    private static final long serialVersionUID = -7274571682393963583L;

    private String id;

    private String host;

    private Long startTime;
    private Long endTime;
    private Long consumeTime;

    private String requestMethod;
    private String requestState;

    private Boolean successFlag;

    private String pathVariable;

    private String pattern;

    private String requestUrl;
    private String queryString;
    private String fullRequestUrl;

    private String clazz;

    private String method;

    private String currentUserId;
    private String currentDealerId;

    private String currentUserName;


    private String paramsJson;

    private Date startDate;
    private Date endDate;

    private String referer;
    private String body;
    private String sessionId;



    public AccessLog() {
    }


    public Long getConsumeTime() {
        if(consumeTime == null){
            if(startTime == null){
                return null;

            }
            if(endTime == null){
                endTime = System.nanoTime();
            }

            consumeTime = endTime - startTime;
        }

        return consumeTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host == null ? null : host.trim();
    }


    public void setConsumeTime(Long consumeTime) {
        this.consumeTime = consumeTime;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod == null ? null : requestMethod.trim();
    }

    public Boolean getSuccessFlag() {
        return successFlag;
    }

    public void setSuccessFlag(Boolean successFlag) {
        this.successFlag = successFlag;
    }

    public String getPathVariable() {
        return pathVariable;
    }

    public void setPathVariable(String pathVariable) {
        this.pathVariable = pathVariable == null ? null : pathVariable.trim();
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern == null ? null : pattern.trim();
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl == null ? null : requestUrl.trim();
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz == null ? null : clazz.trim();
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method == null ? null : method.trim();
    }

    public String getCurrentUserId() {
        return currentUserId;
    }

    public void setCurrentUserId(String currentUserId) {
        this.currentUserId = currentUserId;
    }

    public String getCurrentUserName() {
        return currentUserName;
    }

    public void setCurrentUserName(String currentUserName) {
        this.currentUserName = currentUserName == null ? null : currentUserName.trim();
    }


    public String getParamsJson() {
        return paramsJson;
    }

    public void setParamsJson(String paramsJson) {
        this.paramsJson = paramsJson == null ? null : paramsJson.trim();
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    public String getFullRequestUrl() {
        if(StringUtils.isNotBlank(queryString)){
            fullRequestUrl = requestUrl + "?" + queryString;
        }else{
            fullRequestUrl = requestUrl;
        }
        return fullRequestUrl;
    }

    public void setFullRequestUrl(String fullRequestUrl) {
        this.fullRequestUrl = fullRequestUrl;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer
                .append("host: ").append(host)
                .append("\nreferer: ").append(referer)
                .append("\nrequestUrl: ").append(requestUrl)
                .append("\nrequestMethod: ").append(requestMethod)
//                .append("\npattern: ").append(pattern)
//                .append("\nclass: ").append(clazz)
//                .append("\nmethod: ").append(method)
//                .append("\nstartTime: ").append(startTime).append(" nanosecond")
//                .append("\nendTime: ").append(endTime).append(" nanosecond")
                .append("\nconsumeTime: ").append(consumeTime/1000000).append(" ms")
//                .append("\npathVariable: ").append(pathVariable)
                .append("\nparamsJson: ").append(paramsJson)
                .append("\nuserId: ").append(currentUserId)
                .append("\nuserName: ").append(currentUserName)
                .append("\ndealerId: ").append(currentDealerId)
                .append("\nbody: ").append(body);

        return buffer.toString();
    }

    public String getRequestState() {
        return requestState;
    }

    public void setRequestState(String requestState) {
        this.requestState = requestState;
    }


    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }


	public String getCurrentDealerId() {
		return currentDealerId;
	}


	public void setCurrentDealerId(String currentDealerId) {
		this.currentDealerId = currentDealerId;
	}


	public String getBody() {
		return body;
	}


	public void setBody(String body) {
		this.body = body;
	}
	
	

    
    
}